<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class KeuanganControllerSiku extends MX_Controller {

    private $_template = "siku_iainmataram";

    private $request_var;

    function __construct()
	{
		parent::__construct();
        $this->access_library_siku->is_logged_in();
		$this->load->model("keuangan_model");
        $this->load->model("sirak_civitas/sirak_civitas_model");
        $this->load->model("siab_user/user_model");
    }

    function viewInisiasiNeraca()
    {
        $data['viewSpp'] = $this->keuangan_model->getViewSpp();
        $data['angkatan'] = $this->system_model->getSubstringNim();

        $data['page'] = 'lihat_spp';
        $this->load->view($this->_template,$data);
    }

    function addSpp()
    {
        $data['angkatan'] = $this->system_model->getSubstringNim();

        if($data['angkatan'])
        {
          if($this->input->post('Nama'))
          {
            $this->keuangan_model->insertSPP($_REQUEST);
            $this->session->set_flashdata('status', 'Data telah disimpan');
            $this->session->set_flashdata('clr', 'p_messages_success');
            redirect('siku/inisiasi.html', 'location');
          }
          else
          {
            $data['options_jalur'] = $this->sirak_civitas_model->getJalurPenerimaan();
            $data['options_program'] = $this->sirak_civitas_model->getTableProgram();
            $data['options_tagihan'] = $this->keuangan_model->getTableTagihan();
            $data['options_prodi'] = $this->system_model->getProdi();

            $data['page'] = 'tambah_spp';
            $this->load->view($this->_template,$data);
          }
        }
        else
        {
          echo "Belum ada data mahasiswa. Silakan masukkan data mahasiswa melalui Sistem Informasi Registrasi Akademik. Terima kasih.";
        }
    }

    function detailSpp($angsuranKe,$idSpp)
    {
        $data['detailSpp'] = $this->keuangan_model->selectSppById($idSpp);

        if($data['detailSpp']==NULL)
          show_404();

        $data['rincianAngsuran'] = $this->keuangan_model->selectSppByTagihan($data['detailSpp']->ANGKATAN,$data['detailSpp']->STATUS_PENERIMAAN,$data['detailSpp']->PROGRAMID,$angsuranKe);

        $data['passingInput'] = $angsuranKe;

        $data['page'] = 'detail_angsuran';
        $this->load->view($this->_template,$data);
    }

    function addInisiasiNeraca()  //resign
    {
        $this->request_var = $_REQUEST;

        if($this->request_var == NULL){
          echo "No direct script allowed";
          exit();
        }

        $status = $this->keuangan_model->insertNeraca($this->request_var);

        if($status){
          redirect('siku/inisiasi.html', 'location');
        }
        else{
          redirect('siku/inisiasi.html/error_found', 'location');
        }
    }

    function editInisiasiNeraca($id = 0)
    {
        $data['viewSpp'] = $this->keuangan_model->getViewSpp();
        $data['angkatan'] = $this->system_model->getSubstringNim();
        $data['id'] = $id;

        $data['page'] = 'edit_neraca';
        $this->load->view($this->_template,$data);
    }

    function viewTagihan($status_sending=-1)
    {
        $data['viewSpp'] = $this->keuangan_model->getViewSpp();
        $data['optionsTahun'] = $this->system_model->getSelectOptionsValueTahunAjaran();
        $data['optionsSemester'] = $this->system_model->getSelectOptionsValueSemester();

        switch ($status_sending)
        {
          case 1: $data['error'] = FALSE; $data['message'] = "Tagihan berhasil dikirimkan"; break;
          case 2: $data['error'] = TRUE; $data['message'] = "Data mahasiswa tidak ditemukan. Periksa kembali data administrasi mahasiswa."; break;
          case 3: $data['error'] = TRUE; $data['message'] = "Tidak ada mahasiswa yang diberi Hak Akses SIAM. Periksa kembali <b>Pengaturan Pengguna SIA</b> pada SIRAK."; break;
          case 0: $data['error'] = TRUE; $data['message'] = "Tagihan pada semester dan tahun tersebut sudah dikirimkan"; break;
          default: $data['error'] = FALSE; $data['message'] = FALSE; break;
        }

        $data['page'] = 'lihat_tagihan';
        $this->load->view($this->_template,$data);
    }

    function kirimTagihan()
    {
        $this->request_var = $_REQUEST;

        if($this->request_var['sk'] == NULL){
          echo "No direct script allowed";
          exit();
        }

        $status = $this->keuangan_model->insertTagihan($this->request_var);

        $this->session->set_flashdata('tahun_kirim', $this->request_var['tahun']);

        if($status == 1){
          redirect('siku/tagihan.html/sent', 'location');
        }
        elseif($status == 2){
          redirect('siku/tagihan.html/null_data', 'location');
        }
        elseif($status == 3){
          redirect('siku/tagihan.html/hak_akses_mahasiswa_problems', 'location');
        }
        elseif($status == 0){
          redirect('siku/tagihan.html/rejected', 'location');
        }
    }

    function validasiTagihan()
    {
        $data['page'] = 'validasi_tagihan';
        $this->load->view($this->_template,$data);
    }

    function detailTagihan()
	{
		$data['biodata'] = $this->keuangan_model->getViewBiodata($this->input->post('nim'));
		$data['tagihan'] = $this->keuangan_model->getViewDetailNeraca($this->input->post('nim'));
        $data['skripsi'] = $this->keuangan_model->getTagihanSkripsi($this->input->post('nim'));
        $data['kkn'] = $this->keuangan_model->getTagihanKkn($this->input->post('nim'));
        $data['ppl'] = $this->keuangan_model->getTagihanPpl($this->input->post('nim'));
		$data['page'] = 'detail_tagihan';
        $this->load->view($this->_template,$data);
	}

    function updateTagihan()
	{
        $data['biodata'] = $this->keuangan_model->getViewBiodata($this->input->post('nim'));
		$data['tagihan'] = $this->keuangan_model->getViewDetailNeraca($this->input->post('nim'));
        $noTagihan=explode("-",$this->input->post('bayar'));
		switch($noTagihan[0])
		{
			case 1:
			{
				$this->keuangan_model->updateStatusMahasiswa($this->input->post('nim'),1);
                $this->keuangan_model->updateDataNeraca($noTagihan[1]);
				break;
			}
			case 2:
			{
				$this->keuangan_model->updateStatusMahasiswa($this->input->post('nim'),3);
                $this->keuangan_model->updateDataNeraca($noTagihan[1]);
				break;
			}
			case 3:
			{
				$this->keuangan_model->updateStatusMahasiswa($this->input->post('nim'),5);
                $this->keuangan_model->updateDataNeraca($noTagihan[1]);
				break;
			}
            case 4:
            {
                $this->keuangan_model->insertTagihanLain($this->input->post('nim'),2);
                break;
            }
            case 5:
            {
                $this->keuangan_model->insertTagihanLain($this->input->post('nim'),3);
                break;
            }
            case 6:
            {
                $this->keuangan_model->insertTagihanLain($this->input->post('nim'),1);
                break;
            }
			default:
			{
				$this->keuangan_model->updateStatusMahasiswa($this->input->post('nim'),0);
				break;
			}
		}

        if($noTagihan>=1 || $noTagihan<=6)
        {
          $this->session->set_flashdata('status', 'Data telah disimpan');
          $this->session->set_flashdata('clr', 'p_messages_success');
        }
        else
        {
          $this->session->set_flashdata('status', 'Data gagal disimpan');
          $this->session->set_flashdata('clr', 'p_messages_error');
        }

        redirect('siku/validasi-tagihan.html', 'location');

    }

    function viewInisiasiTagihanLain()
    {
        $data['viewTagihanLain'] = $this->keuangan_model->getViewTagihanLain();

        $data['page'] = 'lihat_tagihan_lainnya';
        $this->load->view($this->_template,$data);
    }

    function updateTagihanLain()
    {
        $this->keuangan_model->updateTagihanLain();
        $this->session->set_flashdata('status', 'Data telah disimpan');
        $this->session->set_flashdata('clr', 'p_messages_success');
        redirect('siku/inisiasi-tagihan-lainnya.html', 'location');
    }

    function deleteSppById($idSpp)
    {
       $this->keuangan_model->deleteNeraca($idSpp);
    }

    function deleteSppRelated($idSpp)
    {
       $this->keuangan_model->deleteSppRelated($idSpp);
    }

    function viewPotongan()
    {
        if($this->input->post('nim'))
        {
            $data['biodata'] = $this->user_model->getViewBiodata($this->input->post('nim'));
            $status_sistem = $this->system_model->getViewKontrolData_limit1();
            $data['potongan'] = $this->keuangan_model->selectPotonganByNimWhereTahunSemester($this->input->post('nim'),$status_sistem->KODE_TAHUN,$status_sistem->KODE_SEMESTER);


            $data['page'] = 'lihat_potongan';
            $this->load->view($this->_template,$data);
        }
        else
        {
            $data['page'] = 'cari_mhs_potongan';
            $this->load->view($this->_template,$data);
        }
    }

    function viewPotongan_NimUrl($nim)
    {

        $data['biodata'] = $this->user_model->getViewBiodata($nim);
        $status_sistem = $this->system_model->getViewKontrolData_limit1();
        $data['potongan'] = $this->keuangan_model->selectPotonganByNimWhereTahunSemester($nim,$status_sistem->KODE_TAHUN,$status_sistem->KODE_SEMESTER);


        $data['page'] = 'lihat_potongan';
        $this->load->view($this->_template,$data);
    }

    function addPotongan()
    {
        $nim = explode (".",$this->uri->segment(3));
        $nim = $nim[0];
        $data['biodata'] = $this->user_model->getViewBiodata($nim);

        if($this->input->post('tagihan'))
        {
          $this->keuangan_model->insertPotongan($_REQUEST);
          redirect('siku/PotonganSpp/'.$_REQUEST['nim'].'.do', 'location');
        }
        else
        {
          $data['options_tagihan'] = $this->keuangan_model->getTableTagihan();
          $data['status_sistem'] = $this->system_model->getViewKontrolData_limit1();

          $data['page'] = 'tambah_potongan';
          $this->load->view($this->_template,$data);
        }
    }

    function editPotongan($id)
    {
        $data['biodata'] = $this->user_model->getViewBiodata($this->uri->segment(3));

        if($this->input->post('tagihan'))
        {
          $this->keuangan_model->updatePotongan($this->input->post('id'));
          redirect('siku/PotonganSpp/'.$this->input->post('nim').'.do', 'location');
        }
        else
        {
          $data['options_tagihan'] = $this->keuangan_model->getTableTagihan();
          $data['status_sistem'] = $this->system_model->getViewKontrolData_limit1();
          $data['potongan'] = $this->keuangan_model->selectPotonganById($id);

          if($data['potongan']==NULL) show_404();

          $data['page'] = 'edit_potongan';
          $this->load->view($this->_template,$data);
        }
    }

    function deletePotongan($id)
    {
       $this->keuangan_model->deletePotongan($id);
    }
}

